// zh_gjw/pages/cuscenter/ziliao.js
const app = getApp()
var imgArray = []
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		var siteUrl = wx.getStorageSync('siteUrl')
    this.setData({
      siteUrl: siteUrl
    })
    this.panduan()
  },

  // 提交判断
  panduan:function(e){
    var openid = wx.getStorageSync('openid')
    app.util.request({
      url:'entry/wxapp/Editcusis',
      data:{
				phone: wx.getStorageSync('adminlogin')
      },
      success:function(res){
        if(res.data.code==200){
          wx.showModal({
            title: '提示',
            content: '您已经完善过资料，可在定制商家资料中查看，点击确定可在本页面重新上传',
            success:function(res){
              if(res.confirm){
              }else if(res.cancel){
                wx.navigateBack({
                  delta:1
                })
              }
            }
          })
        }
      }
    })
  },

  arrImg: function (e) {
    var that = this
		var siteUrl = wx.getStorageSync('siteUrl')
    var str
    var img_length = 5 - imgArray.length
    if (img_length > 0 && img_length < 6) {
      wx.chooseImage({
        count: 5, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths
          that.upload({
						url: wx.getStorageSync('siteUrl') + 'app/index.php?i=' + wx.getStorageSync('uniacid') + '&c=entry&a=wxapp&do=Uploads&m=zh_gjw',
            path: tempFilePaths
          });
        }
      })
    }

  },

  delete_img: function (e) {
    var that = this
    var index = e.currentTarget.dataset.index
    imgArray = imgArray.splice(index, 1)
    that.setData({
      imgArray: imgArray
    })
  },

 
  upload: function (data) {
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'upload_file',
      formData: null,
      success: (resp) => {
        if (resp.data != '') {
          success++;
          imgArray.push(resp.data)
          that.setData({
            imgArray: imgArray
          })
        }
        else {
          wx.showToast({
            icon: "loading",
            title: "请重试"
          })
        }
      },
      fail: (res) => {
        fail++;
      },
      complete: () => {
        i++;
        if (i == data.path.length) {
          that.setData({
            images: data.path
          });
          wx.hideToast();
        } else {
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.upload(data);
        }

      }
    });
  },

  sub:function(e){
    var that=this
    var openid = wx.getStorageSync('openid')
    var imgArray = that.data.imgArray
    var info=e.detail.value.info
    if (imgArray==undefined){
      wx.showToast({
        title: '请至少上传一张轮播图',
        icon:'none',
      })
    }else if(info==""){
      wx.showToast({
        title: '请填写商家介绍',
        icon: 'none',
      })
    }else{
      imgArray = that.data.imgArray.join(',')
      app.util.request({
        url:'entry/wxapp/Editcus',
        data:{
					phone: wx.getStorageSync('adminlogin'), bannerimg: imgArray, storedetail:info
        },
        success:function(res){
          if(res.data.code==200){
            wx.showModal({
              title: '提示',
              content: '上传成功，可在定制商家资料中查看',
              showCancel:'fasle',
              success:function(res){
                if(res.confirm){
                  wx.navigateTo({
                    url: '../cuscenter/cuscenter',
                  })
                }
              }
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})